package com.second.service.impl;

import com.second.consts.OrderStatus;
import com.second.consts.PersonalConst;
import com.second.mapper.*;
import com.second.model.dto.reviews.FUserCommentDto;
import com.second.model.dto.storeInfo.FShopFractionDto;
import com.second.model.pojo.*;
import com.second.service.IFCommentService;
import com.second.service.IFShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 本类用来演示:
 *
 * @author: MuTing
 * @date: 2022-04-17 15:45
 */
@Service
@Transactional
public class FCommentServiceImpl implements IFCommentService {

    @Autowired
    private FCommentMapper fCommentMapper;

    @Autowired
    private FUserMapper fUserMapper;

    @Autowired
    private IFShopService fShopService;

    @Autowired
    private FCommentZanMapper fCommentZanMapper;

    @Autowired
    private FCommentPictureMapper commentPictureMapper;

    @Autowired
    private FCommentPictureMapper fCommentPictureMapper;

    @Autowired
    private IFOrdMapper ordMapper;

    @Autowired
    private FRecommendVoMapper recommendVoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    DecimalFormat s1 = new DecimalFormat( ".0");


    @Override
    public List<FUserCommentDto> findCommentsByShopId(Integer id,Integer pageNumber) {

        Pageable pageable = PageRequest.of(pageNumber,3, Sort.Direction.DESC, "commentTime");
        Page<FComment> page = fCommentMapper.findByShopId(id,pageable);

        List<FUserCommentDto> fuc = new ArrayList<>();
        for (FComment f : page) {
            FUser user = fUserMapper.getById(f.getUserId());
            System.out.println(user);
            int count = fCommentZanMapper.countByCommentIdAndStatus(f.getId(), PersonalConst.Zan);
            FCommentZan cZan = fCommentZanMapper.getByUserIdAndCommentId(f.getUserId(),f.getId());

            FUserCommentDto fUserCommentDto = new FUserCommentDto();
            fUserCommentDto.setCommentId(f.getId());
            fUserCommentDto.setUserId(user.getId());
            fUserCommentDto.setUserName(user.getUsername());
            fUserCommentDto.setCommentId(f.getId());
            fUserCommentDto.setHeadPicture(user.getHeadPicture());
            fUserCommentDto.setContent(f.getContent());
            fUserCommentDto.setScores(f.getGeneralComment());
            fUserCommentDto.setCommentTime(f.getCommentTime());
            if (null!=cZan){
                fUserCommentDto.setStatus(cZan.getStatus());
            }else fUserCommentDto.setStatus(PersonalConst.Not_Zan);
            fUserCommentDto.setPraise(count);
            fUserCommentDto.setCurrent(pageNumber+1);
            fUserCommentDto.setPage(page.getTotalPages());
            FCommentPicture fCommentPicture = fCommentPictureMapper.getByCommentId(f.getId());
            if (null != fCommentPicture){
                fUserCommentDto.setUrl1(fCommentPicture.getUrl1());
                fUserCommentDto.setUrl2(fCommentPicture.getUrl2());
                fUserCommentDto.setUrl3(fCommentPicture.getUrl3());
                fUserCommentDto.setUrl4(fCommentPicture.getUrl4());
                fUserCommentDto.setUrl5(fCommentPicture.getUrl5());
                fUserCommentDto.setUrl6(fCommentPicture.getUrl6());
            }
            fuc.add(fUserCommentDto);
        }
        return fuc;
    }

    @Override
    public int CountComments(Integer shopId) {
        return fCommentMapper.countByShopId(shopId);
    }

    @Override
    public FComment findById(Integer id) {
        return fCommentMapper.findById(id).get();
    }

    @Override
    public void addComment(FComment fComment,Integer userId) {
        fComment.setCommentTime(new Date());
        fCommentMapper.save(fComment);
        fComment.setPictureId(fComment.getId());
        fCommentMapper.save(fComment);
        FCommentPicture commentPicture = new FCommentPicture();
        //添加图片
        commentPicture.setCommentId(fComment.getId());

        List list=redisTemplate.opsForList().range("commentPicture"+fComment.getUserId()+":"+fComment.getShopId(),0,-1);
        for (int i = 0; i < list.size(); i++) {
            switch (i){
                case 0:
                    commentPicture.setUrl1("https://foodreviews.oss-cn-hangzhou.aliyuncs.com/"+list.get(i));
                    break;
                case 1:
                    commentPicture.setUrl2("https://foodreviews.oss-cn-hangzhou.aliyuncs.com/"+list.get(i));
                    break;
                case 2:
                    commentPicture.setUrl3("https://foodreviews.oss-cn-hangzhou.aliyuncs.com/"+list.get(i));
                    break;
                case 3:
                    commentPicture.setUrl4("https://foodreviews.oss-cn-hangzhou.aliyuncs.com/"+list.get(i));
                    break;
                case 4:
                    commentPicture.setUrl5("https://foodreviews.oss-cn-hangzhou.aliyuncs.com/"+list.get(i));
                    break;
                case 5:
                    commentPicture.setUrl6("https://foodreviews.oss-cn-hangzhou.aliyuncs.com/"+list.get(i));
                    break;
            }
            if (i>5)break;

        }
        while (redisTemplate.opsForList().size("commentPicture"+fComment.getUserId()+":"+fComment.getShopId()) > 0){
            redisTemplate.opsForList().leftPop("commentPicture"+fComment.getUserId()+":"+fComment.getShopId());
        }
        commentPictureMapper.save(commentPicture);
        //将订单状态改为已经评论
        FOrd ord = ordMapper.getById(fComment.getOrderId());
        ord.setOperation(OrderStatus.Order_Review);
        ordMapper.save(ord);
        recommendVoMapper.insertRecommendVo(userId,fComment.getShopId(),fComment.getGeneralComment());
    }

    @Override
    public void updateComment(FComment fComment) {
        fCommentMapper.save(fComment);
    }

    @Override
    public void delByCommentId(Integer id) {
        fCommentMapper.delByCommentId(id);
    }

    @Override
    public FShopFractionDto CommentScoreCounts(Integer id) {
        FShopFractionDto fShopFractionDto = fCommentMapper.ScoreCounts(id);
            fShopService.saveScore(fShopService.getById(id),Double.valueOf(s1.format(null==fShopFractionDto.getScore()?3.0:fShopFractionDto.getScore())));
            fShopFractionDto.setScore(Double.valueOf(s1.format(null==fShopFractionDto.getScore()?3.0:fShopFractionDto.getScore())));
            fShopFractionDto.setFlavor(Double.valueOf(s1.format(null==fShopFractionDto.getFlavor()?3.0:fShopFractionDto.getFlavor())));
            fShopFractionDto.setEnvironment(Double.valueOf(s1.format(null==fShopFractionDto.getEnvironment()?3.0:fShopFractionDto.getEnvironment())));
            fShopFractionDto.setService(Double.valueOf(s1.format(null==fShopFractionDto.getService()?3.0:fShopFractionDto.getService())));
            return fShopFractionDto;
    }

}
